/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.thrift.utility;

import com.vng.zing.survey.thrift.model.AnswerItem;
import com.vng.zing.survey.thrift.model.QuesType;
import com.vng.zing.survey.thrift.model.Question;
import com.vng.zing.survey.thrift.model.QuestionStats;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author kienpd
 */
public class Utility {

    public static List<AnswerItem> trimListAnswers(List<AnswerItem> list) {
        List<AnswerItem> result = new ArrayList<AnswerItem>();
        for (AnswerItem answerItem : list) {
            if (answerItem.getType() == QuesType.SINGLE_LINE || answerItem.getType() == QuesType.MULTI_LINE) {
                if (answerItem.getAnswer().length() > 0) {
                    result.add(answerItem);

                }

            } else {
                if (answerItem.getAnswer().contains("1")) {
                    result.add(answerItem);

                }
            }

        }
        list.clear();
        list.addAll(result);

        return list;
    }

    public static Map<Short, QuestionStats> synchMapQuesStat(Map<Short, QuestionStats> map1, Map<Short, QuestionStats> map2) {
        for (Map.Entry<Short, QuestionStats> entry : map2.entrySet()) {
            System.out.println("Key : " + entry.getKey() + " Value : "
                    + entry.getValue());
            
            if (map1.containsKey(entry.getKey())) {
                map1.get(entry.getKey()).setType(entry.getValue().getType());
                List<Integer> stats1 = map1.get(entry.getKey()).getNumber();
                List<Integer> stats2 = entry.getValue().getNumber();
                int i =0;
                for(Integer item :stats2){
                    stats1.set(i, stats1.get(i)+item);
                    i++;   
                }
                map1.get(entry.getKey()).setNumber(stats1);
            } else {
                 map1.put(entry.getKey(),new QuestionStats(entry.getValue()));

            }

        }

        return map1;
    }
       public static AnswerItem getAnswerItemById(short id, List<AnswerItem> list) {
        AnswerItem answerItem = null;
        for (AnswerItem ai : list) {
            if (ai.getIdQues() == id) {
                answerItem = new AnswerItem(ai);
                return answerItem;
            }
        }

        return answerItem;
    }

     public static boolean validResponse(List<AnswerItem> answers, List<Question> questions) {
        AnswerItem answerItem;
        for (Question question : questions) {
            answerItem = getAnswerItemById(question.getIdQues(), answers);
            if (question.require) {
                if (answerItem == null) {
                    return false;
                }
                if (question.getType() != answerItem.getType()) {
                    return false;
                }
                if ("".equals(answerItem.getAnswer()) || answerItem.getAnswer() == null) {
                    return false;
                }

                if (question.getType() == QuesType.SINGLE_SELECTION || question.getType() == QuesType.MULTI_CHOICE) {
                    String[] ans = answerItem.getAnswer().split(",");
                    if (question.getChoices().size() != ans.length) {
                        return false;
                    }
                    if (!answerItem.getAnswer().contains("1")) {
                        return false;
                    }
                    int i = 0;
                    for (String an : ans) {
                        if (!an.matches("[0-1]")) {
                            return false;
                        }
                        if (an.equals("1") && question.getType() == QuesType.SINGLE_SELECTION) {
                            i++;

                        }
                    }
                    if (question.getType() == QuesType.SINGLE_SELECTION && i > 1) {
                        return false;
                    }

                }

            } else {
                if (answerItem != null) {
                    if (question.getType() != answerItem.getType()) {
                        return false;
                    }

                    if (question.getType() == QuesType.SINGLE_SELECTION || question.getType() == QuesType.MULTI_CHOICE) {
                        String[] ans = answerItem.getAnswer().split(",");
                        if (question.getChoices().size() != ans.length) {
                            return false;
                        }
                        int i = 0;
                        for (String an : ans) {
                            if (!an.matches("[0-1]")) {
                                return false;
                            }
                            if (an.equals("1") && question.getType() == QuesType.SINGLE_SELECTION) {
                                i++;

                            }
                        }
                        if (question.getType() == QuesType.SINGLE_SELECTION && i > 1) {
                            return false;
                        }

                    }

                }
            }

        }

        return true;
    }


}
